<?php

use Overtrue\Pinyin\Pinyin;

if (!function_exists('LogFileSave')) {
    function LogFileSave($file_path,$name,$data)
    {

        if (!file_exists(storage_path('logs/' .date("Ymd")."/".$file_path."/"))) {
            mkdir(storage_path('logs/' .date("Ymd")."/".$file_path."/"), 0775, true);
            chmod(storage_path('logs/' .date("Ymd")."/".$file_path."/"), 0775);
        }
        file_put_contents(storage_path('logs/' .date("Ymd")."/".$file_path."/". $name.'.log'), '[ ' . date('Y-m-d H:i:s', time()) . ' ] [info ]' . "\n" . print_r($data,1) . "\n\n", FILE_APPEND);
    }
}


if(!function_exists("Success")){
    function Success($code,$data): \Illuminate\Http\JsonResponse
    {
        return response()->json([
            'status' => true,
            'code'  => 200,
            'message' => config('errorCode.code')[(int) $code],
            'data'  => $data,
            'success_time'  => date("Y-m-d H:i:s")
        ]);
    }
}

if(!function_exists("Failed")){
    function Failed($code,$data): \Illuminate\Http\JsonResponse
    {
        return response()->json([
            'status' => false,
            'code'  => $code,
            'message' => config('errorCode.code')[$code],
            'data'  => $data,
            'failed_time'  => date("Y-m-d H:i:s")
        ]);
    }
}

if(!function_exists("Error")){
    function Error($code, $message, $data = []): \Illuminate\Http\JsonResponse
    {
        return response()->json([
            'status'       => false,
            'code'         => $code,
            'message'      => $message,
            'data'         => $data,
            'failed_time'  => date("Y-m-d H:i:s")
        ]);
    }
}

if (!function_exists('getFullPinYin')) {
    function getFullPinYin($string): string
    {
        $pinyin = new Pinyin();
        return $pinyin->permalink($string,"");
    }
}

if (!function_exists('getShortPinYin')) {
    function getShortPinYin($string): string
    {
        $pinyin = new Pinyin();
        return $pinyin->abbr($string,"");
    }
}
